package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.pojo.*;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: Alice
 * @Description:
 * @Date 2020/1/9 19:59
 **/
@RestController
@RequestMapping("/Role")
public class RoleController {

    @Reference
    RoleService roleService;

    @RequestMapping("/getRole")
    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    public Result getMenu() {
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        List<Role> menuList = roleService.getRoleByRoleName(username);
        return Result.success("", menuList);
    }

    @RequestMapping("/add")
    @PreAuthorize("hasAnyAuthority('ROLE_ADD')")
    public Result add(@RequestBody Role role) {
        try {
            roleService.add(role);
            return new Result(true, "新增角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增角色失败");
        }
    }


    @RequestMapping("/edit")
    @PreAuthorize("hasAnyAuthority('ROLE_EDIT')")
    public Result edit(@RequestBody Role role) {
        try {
            roleService.edit(role);
            return new Result(true, "编辑角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑角色失败");
        }
    }

    @RequestMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findAll(queryPageBean);
    }

    @RequestMapping("/deleteById")
    @PreAuthorize("hasAnyAuthority('ROLE_DELETE')")
    public Result deleteById(int id) {
        try {
            roleService.deleteById(id);
            return new Result(true, "删除角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除角色失败");
        }
    }


    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    public Result findById(int id) {
        try {
            RolePermissionMenuVo rolePermissionMenuVo =  roleService.findById(id);


            return new Result(true, "回显角色成功",rolePermissionMenuVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "回显角色失败");
        }
    }


    @RequestMapping("/findPermission")
    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    public Result findPermission() {
        try {
            List<Permission> permissionList = roleService.findPermission();
            return new Result(true, "回显权限数据成功",permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "回显权限数据失败");
        }
    }

    @RequestMapping("/findMenus")
    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    public Result findMenus() {
        try {
            List<Menu> menuList = roleService.findMenus();
            return new Result(true, "回显权限数据成功",menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "回显权限数据失败");
        }
    }


}
